import { reqAddressList, reqAddressDel } from "../../../../../api/address";
Page({
  // 页面的初始数据
  data: {
    addressList: [],
    flag: 0, // 判断是否从 提交订单 页面过来的;1代表是从提交订单页面过来的
  },

  // 去编辑页面
  toEdit(event) {
    const id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/modules/settingModule/pages/address/add/index?id=${id}`,
    });
  },

  // 更新收获地址
  toOrderPage(event) {
    if (!this.data.flag) return;

    const addressItem = event.currentTarget.dataset.addressitem;
    console.log("更新收获地址=》", addressItem);
    wx.navigateTo({
      url: `/modules/orderPayModule/pages/order/detail/detail?addressItem=${JSON.stringify(
        addressItem
      )}&flag=1`,
    });
  },

  onShow() {
    this.getAddressList();
  },

  onLoad(options) {
    console.log("地址接收参数=》", options);
    this.data.flag = options.flag;
  },

  // 收获地址列表
  async getAddressList() {
    const { data } = await reqAddressList();
    console.log("收获地址=》", data);
    if (data.code === 200) {
      this.setData({
        addressList: data.data,
      });
    }
  },

  // 删除收获地址
  async deleteBtn(event) {
    const { data } = await reqAddressDel(event.currentTarget.dataset.id);
    console.log("删除=》", data);
    if (data.code === 200) {
      this.getAddressList();
    }
  },
});
